(function() {
  //类型切换
  typeSwitch();
  function typeSwitch() {
    var $typeSwitch = $('#typeSwitch>a');
    var params = getQueryObject();
    // getUrlByQuery
    $typeSwitch.click(function() {
      if ($(this).hasClass('active')) return;
      var type = $(this).attr('type');
      if (type) {
        params.type = type;
      } else {
        delete params.type;
      }
      window.location.href = getUrlByQuery(params);
    });
  }

  //新建文件夹
  newFolder();
  function newFolder() {
    var $newFolder = $('#newFolder');
    $newFolder.click(function() {
      layer.prompt({
        title: '请输入文件夹名称',
        formType: 3
      }, function(pass, index) {
        if (isSpecialStr(pass)) {
          layer.msg('文件夹名称不可有特殊字符~');
          return;
        }
        var folderId = $('#folderId').val();
        var parentFolderId = $('#parentFolderId').val();
        var params = {
          name: pass,
          parent_id: folderId,
          grand_parent_id: parentFolderId
        };
        add(params);
        layer.close(index);
      });
    });

    function add(data) {
      $.ajax({
        url: '/admin/folder/add',
        data: data,
        success: function(data) {
          window.location.reload();
        },
        fail: function(err) {
          layer.msg(err)
        }
      });
    }
  }

  //搜索
  search();
  function search() {
    var $submitSearch = $('#submitSearch');
    var $input = $('#keyword');
    $submitSearch.click(function() {
      var value = $input.val();
      // if (value) {
        var urlObject = getQueryObject();
        urlObject.name = value;
        window.location.href = getUrlByQuery(urlObject);
      // } else {
      //   layer.tips('关键词不能为空哦~', '#keyword', {
      //     tips: [1, '#1ab394']
      //   });
      // }
    });
  }

  //编辑文件夹
  editFolder()
  function editFolder() {
    var $editFolder = $('.edit_folder');
    $editFolder.click(function() {
      var folderId = $(this).attr('folderId');
      var folderName = $(this).attr('folderName');
      layer.prompt({
        title: '请输入修改文件夹名称',
        formType: 3,
        value: folderName
      }, function(pass, index) {
        if (isSpecialStr(pass)) {
          layer.msg('文件夹名称不可有特殊字符~');
          return;
        }
        $.ajax({
          url: '/admin/folder/edit',
          data: {
            folderId: folderId,
            name: pass
          },
          success: function(data) {
            publishHandler(data)
          },
          fail: function(err) {
            layer.msg(err)
          }
        });
        layer.close(index);
      }, function() {})
    });
  }

  //删除文件夹
  deleteFolder();
  function deleteFolder() {
    var $editFolder = $('.delete_folder');
    $editFolder.click(function() {
      var folderId = $(this).attr('folderId');
      layer.confirm('确认删除该文件夹吗？', {
        btn: ['确定', '取消'] //按钮
      }, function() {
        $.ajax({
          url: '/admin/folder/clear',
          data: {
            folderId: folderId
          },
          success: function(data) {
            publishHandler(data)
          },
          fail: function(err) {
            layer.msg(err)
          }
        });
      }, function() {});
    });
  }

  //编辑文件
  editFile();
  function editFile(){
    var $editFile = $('.edit_file');
    $editFile.click(function(){
      var fileId = $(this).attr('fileId');
      var fileName = $(this).attr('fileName');
      layer.prompt({
        title: '请输入修改文件名称',
        formType: 3,
        value: fileName
      }, function(pass, index) {
        if (isSpecialStr(pass)) {
          layer.msg('文件夹名称不可有特殊字符~');
          return;
        }
        $.ajax({
          url: '/admin/resource/edit',
          data: {
            fileId: fileId,
            name: pass
          },
          success: function(data) {
            publishHandler(data)
          },
          fail: function(err) {
            layer.msg(err)
          }
        });
        layer.close(index);
      }, function() {})
    });
  }

  //删除文件
  deleteFile();
  function deleteFile() {
    var $deleteFile = $('.delete_file');
    $deleteFile.click(function() {
      var fileId = $(this).attr('fileId');
      layer.confirm('确认删除该文件吗？', {
        btn: ['确定', '取消'] //按钮
      }, function() {
        $.ajax({
          url: '/admin/resource/clear',
          data: {
            fileId: fileId
          },
          success: function(data) {
            publishHandler(data)
          },
          fail: function(err) {
            layer.msg(err)
          }
        });
      }, function() {});
    });
  }

  //复制连接
  clipUrl();
  function clipUrl() {
      $(".clipUrl").each(function() {
          var fileId = $(this).attr('fileId');
          var clipBtnId = $(this).attr('id');
          var clipTarget = $('#copy_target_' + fileId);
          var config = {
              tips: [1, '#1ab394'],
              time: 2000
          }
          var clip = new Clipboard('#' + clipBtnId);
          clip.on('success', function(e) {
              //layer_show_
              layer.tips('复制成功', '#clip_borad_' + fileId, config);
              e.clearSelection();
          });
          clip.on('error', function(e) {
              layer.tips('复制失败', '#clip_borad_' + fileId, config);
              e.clearSelection()
          });
      });
  }
  function publishHandler(data) {
    if(data.errCode == 0) {
      window.location.reload();
    } else {
      layer.msg(data.errMsg);
    }
  }

  //复选框操作
  checkboxOpt();
  function checkboxOpt() {
    var query = getQueryObject();
    var select = parseInt(query.select);
    var selectIds = [];//以被选择的文件id
    var $checkBox = $('.check-box');
    var $confirmSelect = $('#confirm-select');
    $checkBox.click(function(){
      if($(this).hasClass('checked')) {
        $(this).empty().removeClass('checked');
      } else {
        if(select - selectIds.length == 0) {
          layer.msg('最多只能选择'+ select +'个文件哦~');
          return;
        }
        $(this).html('<i class="fa fa-check" aria-hidden="true"></i>').addClass('checked');
      }
      getSelectIds();
      if(selectIds.length == 0) {
        $confirmSelect.html('确认选择').addClass('disabled');
      } else {
        $confirmSelect.removeClass('disabled').html('确认选择' + selectIds.length + '个文件');
      }
    });
    
    //初始化复选框
    initBoxs();
    function initBoxs(){
      var selectStr = $(window.parent.document.body).find('#saveArticle').attr('fileIds');
      if(!selectStr)  return;
      var selects = selectStr.split(",");
      $(selects).each(function(){
        $('.check-box[fileId='+ this +']').click();
      });
    }

    $confirmSelect.click(function(){
      if($(this).hasClass('disabled')) {
        return;
      }
      window.parent.fileChooseHandler({
        ids: selectIds,
        files: getSelectedInfos()
      });
    });

    function getSelectedInfos(){
      var info = [];
      $checkBox.each(function(){
        if($(this).hasClass('checked')) {
          var obj = {};
          obj.fileId = $(this).attr('fileId');
          obj.filename = $(this).attr('filename');
          obj.url = $(this).attr('url');
          obj.type = $(this).attr('type');
          obj.mime = $(this).attr('mime');
          info.push(obj);
        }
      });
      return info;
    }

    //获取所有被选择的文件id
    function getSelectIds(){
      selectIds = [];
      $checkBox.each(function(){
        if($(this).hasClass('checked')) {
          selectIds.push($(this).attr('fileId'));
        }
      });
    }
  } 
}())